package com.jht.engine.platsign;

import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;
import android.util.Log;
import com.jht.engine.platsign.ILoginService;
import com.jxw.constant.Constant;
import com.jxw.engine.platsign.MainActivity;
import com.jxw.launcher.util.Logger;
import com.jxw.launcher.util.Util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.TreeMap;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONException;
import org.json.JSONObject;

/* loaded from: classes.dex */
public class LoginService extends Service {
    private final ILoginService.Stub mBinder = new ILoginService.Stub() { // from class: com.jht.engine.platsign.LoginService.1
        @Override // com.jht.engine.platsign.ILoginService
        public String getToken() throws RemoteException {
            return LoginService.this.readToken();
        }

        @Override // com.jht.engine.platsign.ILoginService
        public void reLogin() throws RemoteException {
            LoginService.this.login();
        }
    };

    @Override // android.app.Service
    public void onRebind(Intent intent) {
    }

    @Override // android.app.Service
    public boolean onUnbind(Intent intent) {
        return true;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public String readToken() {
        return getSharedPreferences(MainActivity.PREFERENCE, 0).getString("token", null);
    }

    @Override // android.app.Service
    public void onCreate() {
        super.onCreate();
        String readToken = readToken();
        if (readToken == null) {
            login();
        } else {
            checkToekn(readToken);
        }
        Log.e(NotificationCompat.CATEGORY_SERVICE, "LoginService");
        if (Build.VERSION.SDK_INT >= 26) {
            startForeground(1, new Notification());
        }
    }

    @Override // android.app.Service
    public int onStartCommand(Intent intent, int i, int i2) {
        String readToken = readToken();
        if (readToken == null) {
            login();
            return 1;
        }
        checkToekn(readToken);
        return 1;
    }

    @Override // android.app.Service
    public IBinder onBind(Intent intent) {
        return this.mBinder;
    }

    @Override // android.app.Service
    public void onDestroy() {
        stopForeground(true);
    }

    public void login() {
        new Thread(new Runnable() { // from class: com.jht.engine.platsign.LoginService.2
            @Override // java.lang.Runnable
            public void run() {
                final SharedPreferences sharedPreferences = LoginService.this.getSharedPreferences(MainActivity.PREFERENCE, 0);
                String string = sharedPreferences.getString(MainActivity.CUSTOMISBN, "");
                String string2 = sharedPreferences.getString("series", "");
                String str = System.currentTimeMillis() + "";
                try {
                    HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(Util.API_HOST + "/api/jxwuser/timestamp").openConnection();
                    if (httpURLConnection.getResponseCode() == 200) {
                        InputStream inputStream = httpURLConnection.getInputStream();
                        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
                        StringBuilder sb = new StringBuilder();
                        while (true) {
                            String readLine = bufferedReader.readLine();
                            if (readLine == null) {
                                break;
                            }
                            sb.append(readLine + "\n");
                        }
                        inputStream.close();
                        String substring = sb.substring(sb.indexOf("{"), sb.indexOf("}") + 1);
                        Logger.getLogger().e(" timestamp=" + str);
                        if (substring != null) {
                            try {
                                str = new JSONObject(substring).optString("data");
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                } catch (MalformedURLException e2) {
                    e2.printStackTrace();
                } catch (IOException e3) {
                    e3.printStackTrace();
                }
                String genrateKey = SignUtil.genrateKey();
                TreeMap treeMap = new TreeMap();
                treeMap.put(MainActivity.APKISBN, string);
                treeMap.put("series", string2);
                treeMap.put("timestamp", str);
                treeMap.put("nonce", genrateKey);
                treeMap.put("appsecret", Constant.SIGN_KEY);
                String signature = SignUtil.signature(treeMap);
                Logger.getLogger().e("自动注册登录 signature=" + signature + ",items=" + treeMap);
                String str2 = Util.API_HOST + "/api/jxwuser/login?apkIsbn=" + string + "&series=" + string2 + "&timestamp=" + str + "&nonce=" + genrateKey + "&signature=" + signature;
                Log.i("自动注册登录 ", "url_____ " + str2);
                new OkHttpClient().newCall(new Request.Builder().url(str2).build()).enqueue(new Callback() { // from class: com.jht.engine.platsign.LoginService.2.1
                    @Override // okhttp3.Callback
                    public void onFailure(Call call, IOException iOException) {
                        Log.d("自动注册登录", "login onFailure=" + iOException.getMessage());
                    }

                    @Override // okhttp3.Callback
                    public void onResponse(Call call, Response response) throws IOException {
                        Log.d("自动注册登录", "login =" + response);
                        if (response.isSuccessful()) {
                            try {
                                JSONObject jSONObject = new JSONObject(response.body().string());
                                Log.d("自动注册登录", "json =" + jSONObject);
                                String string3 = jSONObject.getJSONObject("data").getString("token");
                                if (TextUtils.isEmpty(string3)) {
                                    return;
                                }
                                Log.e("lyx", "自动注册登录-keep-" + string3);
                                sharedPreferences.edit().putString("token", string3).apply();
                            } catch (JSONException e4) {
                                e4.printStackTrace();
                            }
                        }
                    }
                });
            }
        }).start();
    }

    public void checkToekn(String str) {
        if (TextUtils.isEmpty(str)) {
            return;
        }
        new OkHttpClient().newCall(new Request.Builder().url(Util.API_HOST + "/api/jxwuser/checktoken?token=" + str).build()).enqueue(new Callback() { // from class: com.jht.engine.platsign.LoginService.3
            @Override // okhttp3.Callback
            public void onFailure(Call call, IOException iOException) {
                Log.d("注册核实登录", "checkToekn onFailure=" + iOException.getMessage());
            }

            @Override // okhttp3.Callback
            public void onResponse(Call call, Response response) throws IOException {
                Log.d("注册核实登录", "checkToekn =" + response);
                if (response.isSuccessful()) {
                    try {
                        if (new JSONObject(response.body().string()).getInt("code") == 401) {
                            LoginService.this.login();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }
}
